﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.CanGongService;
using CommunitySystem.Views;

namespace CommunitySystem
{
    public partial class CanGongWindow : ChildWindow
    {
        private CanGongModel model = new CanGongModel();
        public CanGongService.CanGongServiceClient client = new CanGongServiceClient();
        public int ID;
        public bool IsNew = false;
        public CanGongWindow(int id, bool isnew)
        {
            InitializeComponent();
            ID = id;
            IsNew = isnew;
            client.GetCanGongModelCompleted+=new EventHandler<GetCanGongModelCompletedEventArgs>(client_GetCanGongModelCompleted);
            client.InsertCanGongCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_InsertCanGongCompleted);
            client.UpdateCanGongCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_UpdateCanGongCompleted);
            if (IsNew)
            {
                model = new CanGongModel();
                this.DataContext = model;
            }
            else
                client.GetCanGongModelAsync(id, "");

        }


        void client_GetCanGongModelCompleted(object sender, GetCanGongModelCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Count > 0)
            {
                model = e.Result[0];
                this.DataContext = model;
            }
        }

        private int oldshangjiorgid = 0;
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            orgid.SelectItem = null;
            oldshangjiorgid = model.ShangJiOrgID;
            model.ShangJiOrgID = 0;
            model.ShangJiName = ((CheckBox) sender).Content.ToString();
        }

        private void chkHiddenCols_Unchecked(object sender, RoutedEventArgs e)
        {
            model.ShangJiOrgID = oldshangjiorgid;
            model.ShangJiName = "";
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(model.Name))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请填写单位名称！");
                return;
            }
            if (string.IsNullOrEmpty(model.ShangJiName) && model.ShangJiOrgID == 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请填写上级主管部门！");
                return;
            }
            if (IsNew)
                client.InsertCanGongAsync(model);
            else
                client.UpdateCanGongAsync(model);
        }

        void client_UpdateCanGongCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
            //result();
            this.DialogResult = true;
        }

        void client_InsertCanGongCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
            //result();
            this.DialogResult = true;
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
